package com.yonyou.esn.ulink;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.vo.ml.Language;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yonyou.nccloud.gateway.service.ICloudProcessService;

@SuppressWarnings("unchecked")
public class LightAppService implements ICloudProcessService {

    @SuppressWarnings("rawtypes")
	private static  Map classmap = new HashMap();
	private static Map<String,String> langMap = new HashMap<String, String>();
    static{
    	classmap.put("int",Integer.TYPE);
    	classmap.put("long",Long.TYPE);
    	classmap.put("float",Float.TYPE);
    	classmap.put("double",Double.TYPE);
    	classmap.put("boolean",Boolean.TYPE);
    	langMap.put("zhs", Language.SIMPLE_CHINESE_CODE);
    	langMap.put("en", Language.ENGLISH_CODE);
    	langMap.put("zht", Language.TRAD_CHINESE_CODE);
    	
    }
    
	public String process(String arg0) throws Exception {
		JSONObject  jsonObject = JSON.parseObject(arg0);
		String methoid = jsonObject.getString("method");
		Object object =   getNCLocater(jsonObject.getString("className"));
		initInvocationInfo(jsonObject.getJSONObject("invocationInfo"));
	       JSONObject jsonObject1 = paramValue(jsonObject.getJSONArray("paramType"),jsonObject.getJSONObject("param"));
	       Object obj= null;
	       JSONArray paramJArray = jsonObject1.getJSONArray("param");
		if(paramJArray!=null&&paramJArray.size()>0){
	           Method method =  object.getClass().getDeclaredMethod(methoid,paramJArray.toArray(new Class[paramJArray.size()]));
	           obj = method.invoke(object,jsonObject1.getJSONArray("value").toArray());
	       }else{
	           Method method = object.getClass().getDeclaredMethod(methoid);
	           obj = method.invoke(object);
	       }    
		return JSON.toJSONString(obj);
	}
	private Class getClass(Object key)throws Exception{
	     if(classmap.containsKey(key))return (Class)classmap.get(key);
	      return  Class.forName((String)key);
	   }
	 private Object getNCLocater(String className){
	        return  NCLocator.getInstance().lookup(className);
	    }
	private JSONObject paramValue(JSONArray paramclass,Map param) throws  Exception{
        JSONArray  paramjsonArray = new JSONArray();
        JSONArray  valuejsonArray = new JSONArray();
        JSONObject  jsonObject =  new JSONObject() ;
       Iterator  iterator =  paramclass.iterator();
       while(iterator.hasNext()){
           String[] keys =String.valueOf(iterator.next()).split(":") ;        
           paramjsonArray.add(getClass(keys[1]));
           valuejsonArray.add(JSON.parseObject(JSON.toJSONString(param.get(keys[0])),getClass(keys[1])));
       }
       jsonObject.put("param",paramjsonArray);
       jsonObject.put("value",valuejsonArray);
      return jsonObject;
    }
	
	private void initInvocationInfo  (JSONObject invocationInfo){
		InvocationInfoProxy.getInstance().setUserCode(invocationInfo.getString("ucode"));
		//InvocationInfoProxy.getInstance().setUserDataSource(invocationInfo.getString("userid"));
		if(StringUtils.isNotBlank(invocationInfo.getString("lang"))&&langMap.containsKey(invocationInfo.getString("lang")))
			InvocationInfoProxy.getInstance().setLangCode(langMap.get(invocationInfo.getString("lang")));
		
	}
}
